package net.cyue.jpa;

import java.util.*;


class CYuePersistenceUnitInfoConfigurationSettings {
    public static final String JAKARTA_JDBC_DRIVER = "jakarta.persistence.jdbc.driver";
    public static final String JAKARTA_JDBC_URL = "jakarta.persistence.jdbc.url";
    public static final String JAKARTA_JDBC_USER = "jakarta.persistence.jdbc.user";
    public static final String JAKARTA_JDBC_PASSWORD = "jakarta.persistence.jdbc.password";

    public static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    public static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
    public static final String HIBERNATE_HIGHLIGHT_SQL = "hibernate.highlight_sql";
    public static final String HIBERNATE_LOG_SLOW_QUERY = "hibernate.log_slow_query";
}

public class CYuePersistenceUnitInfoConfiguration {

    public String unitName;
    // public String persistenceProviderClassName = "org.hibernate.jpa.HibernatePersistenceProvider";
    public String persistenceProviderClassName;
    public List<String> managedClassNameList = new ArrayList<>();


    // properties
    public String url;
    public String user;
    public String password;
    public boolean showSQL = true;
    public boolean formatSQL = true;
    public boolean highlightSQL = true;
    public boolean logSlowQuery = false;

    public CYuePersistenceUnitInfoConfiguration(String url) {
        this(UUID.randomUUID().toString(), url);
    }
    public CYuePersistenceUnitInfoConfiguration(String unitName, String url) {
        this(unitName, url, null, null);
    }
    public CYuePersistenceUnitInfoConfiguration(
        String url,
        String user,
        String password
    ) {
        this(UUID.randomUUID().toString(), url, user, password);
    }
    public CYuePersistenceUnitInfoConfiguration(
        String unitName,
        String url,
        String user,
        String password
    ) {
        this.unitName = unitName;
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public CYuePersistenceUnitInfoConfiguration addManagedClass(String className) {
        this.managedClassNameList.add(className);
        return this;
    }

    public Map<String, Object> getPropertiesMap() {
        Map<String, Object> m = new HashMap<>();
        m.put("hibernate.transaction.jta.platform", "org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform");

        if (this.url != null && !this.url.isEmpty()) {
            m.put(CYuePersistenceUnitInfoConfigurationSettings.JAKARTA_JDBC_URL, this.url);
        }
        if (this.user != null && !this.user.isEmpty()) {
            m.put(CYuePersistenceUnitInfoConfigurationSettings.JAKARTA_JDBC_USER, this.user);
        }
        if (this.password != null && !this.password.isEmpty()) {
            m.put(CYuePersistenceUnitInfoConfigurationSettings.JAKARTA_JDBC_PASSWORD, this.password);
        }
        if (this.showSQL) {
            m.put(CYuePersistenceUnitInfoConfigurationSettings.HIBERNATE_SHOW_SQL, true);
        }
        if (this.formatSQL) {
            m.put(CYuePersistenceUnitInfoConfigurationSettings.HIBERNATE_FORMAT_SQL, true);
        }
        if (this.highlightSQL) {
            m.put(CYuePersistenceUnitInfoConfigurationSettings.HIBERNATE_HIGHLIGHT_SQL, true);
        }
        if (this.logSlowQuery) {
            m.put(CYuePersistenceUnitInfoConfigurationSettings.HIBERNATE_LOG_SLOW_QUERY, true);
        }
        return m;
    }
    public Properties getProperties() {
         Properties properties = new Properties();
         properties.putAll(this.getPropertiesMap());
         return properties;
    }
}
